<script setup lang="ts">
import { getCurrentInstance, ref, onBeforeMount } from 'vue'
import { onBeforeRouteLeave } from 'vue-router'
const { proxy } = getCurrentInstance()

/* ---- */
const WXConfig = proxy.$Store.get('WXConfig') || {}

/* --设置微信分享功能-- */
proxy.$WxTool.loadJsdk(WXConfig, (obj) => {
  if (!obj) return
  // ----
  obj.hideMenuItem()
})

/* ---- */
const areas = [{
  name: '全部注册商',
  initCount: -1,
  count: 0,
  page: 'SystemUserList',
  type: 'get',
  url: 'dealer/index',
  data: {}
}, {
  name: '信息未审核',
  initCount: -1,
  count: 0,
  page: 'SystemUserList',
  type: 'get',
  url: 'dealer/index',
  data: {
    params: { 'DealerSearch[is_stoped]': -1 }
  }
}, {
  name: '信息已审核',
  initCount: 0,
  count: 0,
  page: 'SystemUserList',
  type: 'get',
  url: 'dealer/index',
  data: {
    params: { 'DealerSearch[is_stoped]': 1 }
  }
}, {
  name: '申请合作商',
  initCount: 0,
  count: 0,
  page: 'SystemUserList',
  type: 'get',
  url: 'dealer/index',
  data: {
    params: {
      'DealerSearch[is_agent]': 1,
      'DealerSearch[is_saler]': 0,
      'DealerSearch[is_salesman]': 0,
      'DealerSearch[is_exhibition]': 0
    }
  }
}, {
  name: '市场推广员',
  initCount: 0,
  count: 0,
  isCity: true,
  page: 'SystemUserList',
  type: 'get',
  url: 'dealer/index',
  data: {
    params: {
      'DealerSearch[is_agent]': 1,
      'DealerSearch[is_saler]': 1
    }
  }
}, {
  name: '区域运营商',
  initCount: 0,
  count: 0,
  isCity: true,
  page: 'SystemUserList',
  type: 'get',
  url: 'dealer/index',
  data: {
    params: {
      'DealerSearch[is_agent]': 1,
      'DealerSearch[is_salesman]': 1
    }
  }
}, {
  name: '战略合作商',
  initCount: 0,
  count: 0,
  isCity: true,
  page: 'SystemUserList',
  type: 'get',
  url: 'dealer/index',
  data: {
    params: {
      'DealerSearch[is_agent]': 1,
      'DealerSearch[is_exhibition]': 1
    }
  }
}, {
  name: '中国批发商',
  initCount: 0,
  count: 0,
  page: 'SystemUserList',
  type: 'get',
  url: 'dealer/index',
  data: {
    params: { 'DealerSearch[is_warehouse]': 1 }
  }
}]
// ----
const items = ref([])

/* ---- */
const layer = ref()

/* ---- */
const getCurrencyInfo = async () => {
  // --获取对应数量--
  for (let i = 0; i < Number(areas.length); i++) {
    await proxy.$http[areas[i].type](areas[i].url, areas[i].data).then((res) => {
      areas[i].count = res._meta.totalCount + Number(areas[i].initCount)
    }).catch((err) => { proxy.$Myfun.forToast(layer.value, 'open', err) })
  }
  // ----
  areas.forEach((item) => {
    items.value.push({
      show: true,
      child: [{
        show: true,
        isArrow: true,
        name: item.name + '（' + item.count + '）',
        callback: () => {
          const forExtend = { title: item.name, data: item.data.params }
          proxy.$Myfun.openPage(proxy, 'push', item.page, { exTend: JSON.stringify(forExtend), count: item.initCount, isCity: item.isCity })
        }
      }]
    })
  })
}
// ----
const getAuditFun = (type, item) => {
  switch (Number(type)) {
    case 1: {
      item.callback && item.callback(item)
      break
    }
  }
}

/* ---- */
onBeforeMount(() => {
  getCurrencyInfo()
})
onBeforeRouteLeave(() => { proxy.$Myfun.setScrollTop(proxy.$refs.formSection) })
</script>

<template>
  <div class="mescroll-touch" ref="formSection">
    <ul class="userSys xw-flex xw-flex-column">
      <template v-for="(item, index) in items">
      <li v-if="item.show">
        <template v-for="(son, num) in item.child">
        <a class="xw-Tfg xw-flex xw-flex-align" @click="getAuditFun(1, son)" v-if="son.show">
          <div class="USS-title xw-flex-W1">{{son.name}}</div>
          <i class="fal fa-angle-right" v-if="son.isArrow"></i>
        </a>
        </template>
      </li>
      </template>
    </ul>
    <layer-view ref="layer"/>
  </div>
</template>
